<template>
    <!-- ParentDict新增、编辑对话框 -->
    <el-dialog
      :title="!addParentDictForm.dictTypeId ? '新增' : '修改'"
      :visible.sync="addParentDictdialog"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="40%"
      >
      <el-form
        :model="addParentDictForm"
        :rules="addParentDictFormRules"
        ref="addParentDictFormRef"
        label-width="80px"
        :size="size">
        <el-form-item label="分组编码" prop="code">
          <el-input v-model="addParentDictForm.code"></el-input>
        </el-form-item>
        <el-form-item label="分组名称" prop="dictTypeName">
          <el-input v-model="addParentDictForm.dictTypeName"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="6">
            <el-form-item label="状态" prop="status"
              style="margin-left: 0; float: left;">
              <el-switch
                v-model="addParentDictForm.status"
                active-value="1"
                inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="系统字典" prop="isSystem"
              style="margin-left: 0; float: left;">
              <el-switch
                v-model="addParentDictForm.isSystem">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="description">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="addParentDictForm.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="addParentDictdialog = false">取 消</el-button>
        <el-button :size="size" type="primary" @click.native="addParentPassValue">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
import {
  addDictData,
  addDictDataById,
  getDictTypeById
} from '@/api/auth/dict.js'
export default {
  data() {
    return {
      size: 'small',
      addParentDictdialog: false,
      addParentDictForm: {
        dictTypeId: '',
        dictTypeName: '',
        description: '',
        code: '',
        isSystem: 1,
        status: '1'
      },
      addParentDictFormRules: {
        code: [
          { required: true, message: '请输入分组编码', trigger: 'blur' },
          { min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur' }
        ],
        dictTypeName: [
          { required: true, message: '请输入分组名称', trigger: 'blur' },
          { min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur' }
        ],
        description: [
          { min: 0, max: 255, message: '长度在 0 到 255 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(row, flag) {
      // 清空数据
      this.clearForm()
      // 修改
      if (JSON.stringify(row) !== '{}') {
        getDictTypeById(row.dictTypeId).then(res => {
          if (res.code === 200) this.addParentDictForm = res.data
        })
      }
      this.addParentDictdialog = flag
    },
    // 清空表单
    clearForm() {
      this.addParentDictForm.dictTypeId = ''
      this.addParentDictForm.dictTypeName = ''
      this.addParentDictForm.description = ''
      this.addParentDictForm.code = ''
      this.addParentDictForm.isSystem = 1
      this.addParentDictForm.status = '1'
    },
    // 确定提交
    addParentPassValue() {
      this.$refs.addParentDictFormRef.validate(valid => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            if (this.addParentDictForm.dictTypeId !== '') { // 修改
              const data = {
                id: this.addParentDictForm.dictTypeId,
                dictTypeName: this.addParentDictForm.dictTypeName,
                description: this.addParentDictForm.description,
                code: this.addParentDictForm.code,
                isSystem: this.addParentDictForm.isSystem,
                status: this.addParentDictForm.status
              }
              addDictDataById(data).then((res) => {
                this.addParentDictdialog = false
                this.$emit('refreshParentDataList')
              })
            } else { // 新增
              addDictData(this.addParentDictForm).then(res => {
                if (res.code === 200 ) {
                  this.addParentDictdialog = false
                  this.$emit('refreshParentDataList')
                }
              })
            }
          }).catch(() => {})
        }
      })
    }
  }
}
</script>

<style>
</style>
